package com.pug.message.service;

import com.pug.message.config.ConsumerMessageTopicMQConfiguration;
import com.pug.message.mongo.MessageMo;
import com.pug.message.mongo.OrderMessageMo;
import com.pug.message.mongo.UserMessageMo;
import com.pug.message.mongoservice.message.IMessageService;
import com.pug.message.mongoservice.ordermessage.IOrderMessageService;
import com.pug.message.mongoservice.usermessage.IUserMessageService;
import com.pug.message.util.FastJsonUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/4/4$ 20:49$
 */
@Component
@Slf4j
public class MessageConsumer {

    @Autowired
    private IMessageService messageService;
    @Autowired
    private IOrderMessageService orderMessageService;
    @Autowired
    private IUserMessageService userMessageService;

    @RabbitListener(queues = {ConsumerMessageTopicMQConfiguration.PUG_MESSAGE_TOPIC_MONGGO_QUEUE},
            containerFactory = "simpleTopicMessageContainerMununal")
    public void mongoMessage(@Payload Map<String, Object> objectMap, Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        try {
            if(null != objectMap) {
                String msgtype = String.valueOf(objectMap.get("msgtype"));
                if(!StringUtils.isEmpty(msgtype)) {
                    String toJSON = FastJsonUtil.toJSON(objectMap);
                    if (msgtype.equals("1")) {
                        UserMessageMo userMessageMo = FastJsonUtil.toBean(toJSON, UserMessageMo.class);
                        userMessageService.saveMessage(userMessageMo);
                        log.info("pug user message mongodb  save {} routingkey :{} ...............", userMessageMo,receivedRoutingKey);
                    } else if (msgtype.equals("2")) {
                        OrderMessageMo orderMessageMo = FastJsonUtil.toBean(toJSON, OrderMessageMo.class);
                        orderMessageService.saveMessage(orderMessageMo);
                        log.info("pug order message mongodb  save {} routingkey :{} ...............", orderMessageMo,receivedRoutingKey);
                    }
                    channel.basicAck(deliveryTag, false);
                }
            }
        } catch (Exception ex) {
            try {
                log.info("出现异常，可以考虑进行重试....");
                channel.basicReject(deliveryTag, true);
            } catch (Exception ex2) {
                ex2.printStackTrace();
            }
        }
    }

    @RabbitListener(queues = {ConsumerMessageTopicMQConfiguration.PUG_MESSAGE_TOPIC_ES_QUEUE},
            containerFactory = "simpleTopicMessageContainerMununal")
    public void esMessage(@Payload Map<String,Object> objectMap, Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        try {
            if(null != objectMap) {
                String msgtype = String.valueOf(objectMap.get("msgtype"));
                if(!StringUtils.isEmpty(msgtype)) {
                    String toJSON = FastJsonUtil.toJSON(objectMap);
                    if (msgtype.equals("1")) {
                        UserMessageMo userMessageMo = FastJsonUtil.toBean(toJSON, UserMessageMo.class);
                        // es save
                        log.info("pug user message es  save {} routingkey :{} ...............", userMessageMo,receivedRoutingKey);
                    } else if (msgtype.equals("2")) {
                        OrderMessageMo orderMessageMo = FastJsonUtil.toBean(toJSON, OrderMessageMo.class);
                        // es save
                        log.info("pug order message es  save {} routingkey :{} ...............", orderMessageMo,receivedRoutingKey);
                    }
                    channel.basicAck(deliveryTag, false);
                }
            }
        } catch (Exception ex) {
            try {
                log.info("出现异常，可以考虑进行重试....");
                channel.basicReject(deliveryTag, true);
            } catch (Exception ex2) {
                ex2.printStackTrace();
            }
        }
    }
}
